package demo1;

import demo2.MyQueue;

import java.util.Arrays;

// 链栈
public class MyStack1 {

    // 结点
    static class Node {
        public int val;
        public Node next;
        public Node(int val) {
            this.val = val;
        }
    }

    public Node head;
    public int usedSize;

    //压栈
    public void push(int val) {
        Node node = new Node(val);
        if (head == null) {
            head = node;
        } else {
            node.next = head;
            head = node;
        }
        usedSize++;
    }

    //出栈
    public int pop() {
        if(isEmpty()) {
            throw new EmptyException("栈是空的！");
        }
        int ret = head.val;
        head = head.next;
        usedSize--;
        return ret;
    }

    //判断是否为空
    public boolean isEmpty() {
        return usedSize == 0;
    }

    //查看栈顶元素
    public int peek() {
        if(isEmpty()) {
            throw new EmptyException("栈是空的！");
        }
        return head.val;
    }

}
